package health_system.health_system.repository;


import health_system.health_system.entity.ChatRoom;
import health_system.health_system.entity.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface ChatRoomRepository extends JpaRepository<ChatRoom, Long> {
    
    // Find chat rooms by participant
    @Query("SELECT cr FROM ChatRoom cr JOIN cr.participants p WHERE p = :user ORDER BY cr.updatedAt DESC")
    List<ChatRoom> findByParticipant(@Param("user") User user);
    
    // Find chat rooms with pagination
    @Query("SELECT cr FROM ChatRoom cr JOIN cr.participants p WHERE p = :user ORDER BY cr.updatedAt DESC")
    Page<ChatRoom> findByParticipant(@Param("user") User user, Pageable pageable);
    
    // Find one-on-one chat room between two users
    @Query("SELECT cr FROM ChatRoom cr JOIN cr.participants p1 JOIN cr.participants p2 " +
           "WHERE cr.chatType = 'PRIVATE' AND p1 = :user1 AND p2 = :user2")
    Optional<ChatRoom> findOneOnOneChatRoom(@Param("user1") User user1, @Param("user2") User user2);
    
    // Find doctor-patient chat room
    @Query("SELECT cr FROM ChatRoom cr JOIN cr.participants p1 JOIN cr.participants p2 " +
           "WHERE cr.chatType = 'PRIVATE' AND ((p1.id = :doctorId AND p2.id = :patientId) OR " +
           "(p1.id = :patientId AND p2.id = :doctorId))")
    Optional<ChatRoom> findDoctorPatientChatRoom(@Param("doctorId") Long doctorId, @Param("patientId") Long patientId);
    
    // Find group chats for a user
    @Query("SELECT cr FROM ChatRoom cr JOIN cr.participants p " +
           "WHERE p = :user AND cr.chatType = 'GROUP' ORDER BY cr.updatedAt DESC")
    List<ChatRoom> findGroupChatsByParticipant(@Param("user") User user);
    
    // Search chat rooms by name
    @Query("SELECT cr FROM ChatRoom cr JOIN cr.participants p " +
           "WHERE p = :user AND cr.groupName LIKE %:searchTerm% ORDER BY cr.updatedAt DESC")
    List<ChatRoom> searchChatRooms(@Param("user") User user, @Param("searchTerm") String searchTerm);
    
    // Count total number of chat rooms for a user
    @Query("SELECT COUNT(cr) FROM ChatRoom cr JOIN cr.participants p WHERE p = :user")
    Long countByParticipant(@Param("user") User user);
    
    // Find chat rooms with unread messages
    @Query("SELECT DISTINCT cr FROM ChatRoom cr JOIN cr.messages m JOIN m.readStatuses rs " +
           "JOIN cr.participants p WHERE p = :user AND rs.user = :user AND rs.isRead = false " +
           "ORDER BY cr.updatedAt DESC")
    List<ChatRoom> findChatRoomsWithUnreadMessages(@Param("user") User user);
    
    // Count unread chat rooms
    @Query("SELECT COUNT(DISTINCT cr) FROM ChatRoom cr JOIN cr.messages m JOIN m.readStatuses rs " +
           "JOIN cr.participants p WHERE p = :user AND rs.user = :user AND rs.isRead = false")
    Long countChatRoomsWithUnreadMessages(@Param("user") User user);
} 